﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static WorldMapTools.CityGen;

public class ModelGeneratorUtility : MonoBehaviour
{
    private static GameObject roadPrefab;
    private static GameObject roundPrefab;
    private static Material roadMat;

    private static Material roadMatHightway;
    private static Vector3 fromVec = new Vector3(1, 0, 0);
    private static Vector3 upVec = new Vector3(0, 1, 0);
    private const float C_YOffset = 0.001f;

    public static void InitPrefab() {
        roadPrefab = Resources.Load("road_tile_side") as GameObject;
        roundPrefab = Resources.Load("road_corner_low") as GameObject;
        roadMat = Resources.Load("road_tile_mat") as Material;
        roadMatHightway = Resources.Load("road_tile_mat2") as Material;
    }

    public static void GenerateRound(float pointX,float pointZ, RoadType type) {
        GameObject round = GameObject.Instantiate(roundPrefab) as GameObject;
        round.transform.position = new Vector3(pointX, GetYOffset(type)-C_YOffset, pointZ);
        float width = GetRoadWidth(type);
        round.transform.localScale = new Vector3(width, 10, width);
        // round.transform.GetComponent<Renderer>().material = GetRoadMat(type);
        round.transform.GetComponent<Renderer>().sharedMaterial = roadMat;
    }

    public static void GenerateRoad(float pointStartX, float pointStartY, float pointDesX, float pointDesY, RoadType type)
    {
        Vector3 m_pointStart = new Vector3(pointStartX, GetYOffset(type), pointStartY);
        Vector3 m_pointDes = new Vector3(pointDesX, GetYOffset(type), pointDesY);
        //Debug.Log(m_pointStart.ToString() + m_pointDes.ToString());

        Vector3 to = m_pointDes - m_pointStart;
        float distance = Vector3.Distance(m_pointStart, m_pointDes);
        float angle = GetVectorAngle(to);
        GameObject road = GameObject.Instantiate(roadPrefab) as GameObject;
        road.transform.position = m_pointStart;
        road.transform.localEulerAngles = new Vector3(0, -180+angle, 0);
        road.transform.localScale = new Vector3(distance*100, distance / 2, GetRoadWidth(type));
        road.transform.GetComponent<Renderer>().sharedMaterial = GetRoadMat(type);
       // road.transform.GetComponent<Renderer>().sharedMaterial.mainTextureScale= new Vector2(1,distance/2);
    }

    public static Material GetRoadMat(RoadType type) {
        switch (type) {
            case RoadType.HightWay:
                return roadMatHightway;
            case RoadType.Axiom:
                return roadMat;
            default:
                return roadMat;
        }
    }

    public static float GetRoadWidth(RoadType type)
    {
        switch (type)
        {
            case RoadType.HightWay:
                return 100 ;
            case RoadType.Axiom:
                return 50;
            default:
                return 50;
        }
    }

    public static float GetYOffset(RoadType type)
    {
        switch (type)
        {
            case RoadType.HightWay:
                return C_YOffset;
            case RoadType.Axiom:
                return 0;
            default:
                return 0;
        }
    }

    public static float GetVectorAngle(Vector3 toVec) {
        float angle = Vector3.Angle(fromVec, toVec); //求出两向量之间的夹角
        Vector3 normal = Vector3.Cross(fromVec, toVec);//叉乘求出法线向量
        angle *= Mathf.Sign(Vector3.Dot(normal, upVec));
        return angle;
    }

    public void Test() {
        GenerateRoad(797.0942f, 1194.313f, 836.0898f, 1237.5f, RoadType.HightWay);
    }

    private void Start()
    {
        InitPrefab();
        Test();
    }
}

/*
 * 
 * 
 * ModelGeneratorUtility.GenerateRound(vertices[e.v[0]].x * 1200, vertices[e.v[0]].y * 1200);
    switch (e.attri.type) {
        case RoadType.HightWay:
            ModelGeneratorUtility.GenerateRoad(vertices[e.v[0]].x * 1200, vertices[e.v[0]].y * 1200, vertices[e.v[1]].x * 1200, vertices[e.v[1]].y * 1200, RoadType.HightWay);
            break;
        case RoadType.Axiom:
            ModelGeneratorUtility.GenerateRoad(vertices[e.v[0]].x * 1200, vertices[e.v[0]].y * 1200, vertices[e.v[1]].x * 1200, vertices[e.v[1]].y * 1200, RoadType.Axiom);
            break;}
*/

